package untils;

import java.sql.*;

public class SqlCollection {

    protected Connection conn = null;

    //打开连接
    public Connection openConnection() throws Exception {

        //如果conn连接对象为null或者它关闭了，我们在创建新的连接
        if (conn==null || conn.isClosed()){
            //1.加载驱动  Class.forName(“JDBC驱动类名称”)
            Class.forName("com.mysql.cj.jdbc.Driver");  //mysql8.0
            //Class.forName("com.mysql.jdbc.Driver");  //mysql5.0

            //2.获取数据库连接 Connection
            String url = "jdbc:mysql://localhost:3306/trainsystem?serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true"; //mysql 8.0
            //String url = "jdbc:mysql://localhost:3306/selectioncoursesystem"; //mysql5.0
            String userName = "root";  //数据库的用户名，超级管理员
            String password = "root"; //数据库的密码
            conn = DriverManager.getConnection(url,userName,password);
        }
        return conn;

    }


    //关闭资源
    public void closeConnection(Connection conn, PreparedStatement pst, ResultSet rs){
        //如果con连接对象不为null，我们才有必要关闭资源
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (pst!=null){
            try {
                pst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public void closeConnection(Connection conn, PreparedStatement pst){
        //如果con连接对象不为null，我们才有必要关闭资源
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (pst!=null){
            try {
                pst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //关闭conn一个对象
    public void closeConnection(){
        //如果con连接对象不为null，我们才有必要关闭资源
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public void closeConnection(PreparedStatement pst ,ResultSet rs){
        if (pst!=null){
            try {
                pst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public void closeConnection(PreparedStatement pst){
        if (pst!=null){
            try {
                pst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //打开事务
    public void beginTransaction(){
        try {
            //1.打开数据库
            openConnection();
            //2.MySQL中默认事务自动提交，修改为手动提交事务
            conn.setAutoCommit(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //事务提交
    public void commit(){
        try {
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //事务回滚
    public void rollback(){
        try {
            conn.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
